<?php

namespace app\web\controller;


use app\common\service\TicketService;
use app\web\constant\CommonConstant;
use app\web\validate\TicketValidate;
use think\Db;

class Ticket extends Auth
{

    /**
     *  票券管理列表接口
     *
     * @throws \think\exception\DbException
     */
    public function lists()
    {

        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $post = $this->request->post();
            if (empty($post['page']) || empty($post['limit'])) {
                $this->result('缺少参数', '', 204);
            }

            $query = Db::name('qt_ticket')
                ->alias('tk')
                ->field(['tk.*', 'u.real_name'])
                ->where('tk.store_id', $userInfo['store_id'])
                ->where("tk.is_delete", CommonConstant::IS_DELETE_NO)
                ->leftJoin('qt_users u', 'u.id = tk.updater');

            if (!empty($post['name'])) {
                $query->whereLike('tk.name', '%' . $post['name'] . '%');
            }
            $data = $query->order('tk.id desc')->paginate($post['limit']);
            $this->result('查询成功', $data, 200);
        } else {
            $this->result('无效的请求', '', 204);
        }
    }


    /**
     * 票券管理添加票券
     */
    public function add()
    {

        if ($this->request->isPost()) {
            $data = $this->request->post();

            $validate = new TicketValidate();
            if (!$validate->scene('add')->check($data)) {
                $this->result($validate->getError(), '', 204);
            }

            $userInfo = $this->userInfo;
            $data['store_id'] = $userInfo['store_id'];
            $data['creater'] = $data['updater'] = $userInfo['id'];
            $data['create_time'] = $data['update_time'] = time();
            $integralId = Db::name('qt_ticket')->insertGetId($data);
            if ($integralId) {
                $this->result('添加成功', '', 200);
            } else {
                $this->result('添加失败', '', 204);
            }
        }
        $this->result('无效的请求', '', 204);

    }


    /**
     * 编辑票券信息
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function save()
    {

        if ($this->request->isPost()) {
            $data = $this->request->post();

            $validate = new TicketValidate();
            if (!$validate->scene('edit')->check($data)) {
                $this->result($validate->getError(), '', 204);
            }
            $userInfo = $this->userInfo;
            $data['store_id'] = $userInfo['store_id'];
            $data['updater'] = $userInfo['id'];
            $data['update_time'] = time();
            unset($data['create_time']);
            $integralId = Db::name('qt_ticket')->update($data);
            if ($integralId) {
                $this->result('修改成功', '', 200);
            } else {
                $this->result('修改失败', '', 204);
            }
        }
        $this->result('无效的请求', '', 204);

    }


    /**
     * 获取修改票券信息接口
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {

        if ($this->request->isPost()) {
            $ticket_id = $this->request->post('ticket_id');

            $data = TicketService::getTicketById($ticket_id);
            if (!empty($data)) {
                $this->result('获取成功', $data, 200);
            } else {
                $this->result('没有查询到对应ID的数据', '', 204);
            }
        }

        $this->result('无效的请求', '', 204);
    }

    /**
     * 票券删除
     *
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        if ($this->request->isPost()) {
            $ticket_id = $this->request->post('ticket_id');
            if (!$ticket_id) {
                $this->result('无效的请求', '', 204);
            }
            $data = TicketService::getTicketById($ticket_id);
            if (!empty($data)) {
                Db::name('qt_ticket')
                    ->update([
                        'id' => $ticket_id,
                        'is_delete' => CommonConstant::IS_DELETE_YES,
                        'update_time' => time(),
                        'updater' => $this->userInfo['id']
                    ]);

                $this->result('删除成功', '', 200);
            } else {
                $this->result('没有查询到对应ID的数据', '', 204);
            }
        }

        $this->result('无效的请求', '', 204);

    }

}
